package com.profgj.profgj.service.impl;

import com.profgj.profgj.entity.ProductYearEnergyconsumption;
import com.profgj.profgj.dao.ProductYearEnergyconsumptionDao;
import com.profgj.profgj.enums.CalculatedValue;
import com.profgj.profgj.service.ProductYearEnergyconsumptionService;
import com.profgj.profgj.vo.ProductYearEnergyconsumptionVo;
import com.profgj.profgj.vo.ProductYearEnergyconsumptionVoResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static java.lang.Double.NaN;

/**
 * (ProductYearEnergyconsumption)表服务实现类
 *
 * @author makejava
 * @since 2021-07-06 09:40:14
 */
@Service("productYearEnergyconsumptionService")
public class ProductYearEnergyconsumptionServiceImpl implements ProductYearEnergyconsumptionService {
    @Resource
    private ProductYearEnergyconsumptionDao productYearEnergyconsumptionDao;



    /**
     * 产品id和哪年 查询产品一年的月份进行统计年
     * @param productId 产品id
     * @param year      年
     * @return
     */
    @Override
    public ProductYearEnergyconsumptionVo productYearStatisticEnergyByProductId(Long productId, Integer year) {

        // 产品id和年 查询产品的年能耗(是从月分一年的统计出来的部分数据经过下面运算进行填充)
        ProductYearEnergyconsumptionVo productYearEnergyconsumptionVo = productYearEnergyconsumptionDao.productYearStatisticEnergyByProductId(productId, year);

        if(productYearEnergyconsumptionVo == null){
            return null;
        }
        // 算出年的电力转换标准煤
        productYearEnergyconsumptionVo.setElectricityConvCoal(
                CalculatedValue.electricityConvStandardCoal(productYearEnergyconsumptionVo.getYearElectricityConsume()));
        // 算出年的煤炭转换标准煤
        productYearEnergyconsumptionVo.setCoalConvStandardcoal(
                CalculatedValue.coalConvStandardCoal(productYearEnergyconsumptionVo.getYearCoalConsume()));
        // 算出年的天然气转换标准煤
        productYearEnergyconsumptionVo.setNaturalgasConvStandardcoal(
                CalculatedValue.naturalgasConvStandardCoal(productYearEnergyconsumptionVo.getYearNaturalgasConsume()));
        // 算出年额燃料油转换标准煤
        productYearEnergyconsumptionVo.setFueloilConvStandardcoal(
                CalculatedValue.fueloilConvStandardCoal(productYearEnergyconsumptionVo.getYearFueloilConsume()));
        // 算出年的水转换标准煤
        productYearEnergyconsumptionVo.setWaterConvStandardcoal(
                CalculatedValue.waterConvStandardCoal(productYearEnergyconsumptionVo.getYearWaterConsume()));

        // 算出能耗合计
        productYearEnergyconsumptionVo.setEnTotal(
                productYearEnergyconsumptionVo.getElectricityConvCoal() +
                productYearEnergyconsumptionVo.getCoalConvStandardcoal() +
                        productYearEnergyconsumptionVo.getNaturalgasConvStandardcoal() +
                        productYearEnergyconsumptionVo.getFueloilConvStandardcoal() +
                        productYearEnergyconsumptionVo.getWaterConvStandardcoal());

        // 算出单位产品能耗
        Double productEn = productYearEnergyconsumptionVo.getEnTotal() / productYearEnergyconsumptionVo.getYearYield();
        System.out.println(productEn);
        productYearEnergyconsumptionVo.setProductEn(productEn.isNaN() ? 0.0f:productEn);

        // 算出能耗强度
        Double enStrength = productYearEnergyconsumptionVo.getEnTotal() / productYearEnergyconsumptionVo.getYearSalesIncome();
        System.out.println(enStrength);
        productYearEnergyconsumptionVo.setEnStrength(enStrength.isNaN() ? 0.0f :enStrength);

        return productYearEnergyconsumptionVo;
    }

    /**
     * 产品id和关联企业用户id和哪年 查询产品一年的月份进行统计年
     * @param productId 产品id
     * @param userId    关联企业用户id
     * @param year      年
     * @return
     */
    @Override
    public ProductYearEnergyconsumptionVo productYearStatisticEnergyByproductIdAndUserId(Long productId, Integer userId, Integer year) {

        // 产品id和关联企业用户id和年 查询产品的年能耗(是从月分一年的统计出来的部分数据经过下面运算进行填充)
        ProductYearEnergyconsumptionVo productYearEnergyconsumptionVo = productYearEnergyconsumptionDao.productYearStatisticEnergyByproductIdAndUserId(productId, userId, year);

        if (productYearEnergyconsumptionVo == null) {
            return null;
        }

        // 算出年的电力转换标准煤
        productYearEnergyconsumptionVo.setElectricityConvCoal(
                CalculatedValue.electricityConvStandardCoal(productYearEnergyconsumptionVo.getYearElectricityConsume()));
        // 算出年的煤炭转换标准煤
        productYearEnergyconsumptionVo.setCoalConvStandardcoal(
                CalculatedValue.coalConvStandardCoal(productYearEnergyconsumptionVo.getYearCoalConsume()));
        // 算出年的天然气转换标准煤
        productYearEnergyconsumptionVo.setNaturalgasConvStandardcoal(
                CalculatedValue.naturalgasConvStandardCoal(productYearEnergyconsumptionVo.getYearNaturalgasConsume()));
        // 算出年额燃料油转换标准煤
        productYearEnergyconsumptionVo.setFueloilConvStandardcoal(
                CalculatedValue.fueloilConvStandardCoal(productYearEnergyconsumptionVo.getFueloilConvStandardcoal()));
        // 算出年的水转换标准煤
        productYearEnergyconsumptionVo.setWaterConvStandardcoal(
                CalculatedValue.waterConvStandardCoal(productYearEnergyconsumptionVo.getYearWaterConsume()));

        // 算出能耗合计
        productYearEnergyconsumptionVo.setEnTotal(
                productYearEnergyconsumptionVo.getElectricityConvCoal() +
                        productYearEnergyconsumptionVo.getCoalConvStandardcoal() +
                        productYearEnergyconsumptionVo.getNaturalgasConvStandardcoal() +
                        productYearEnergyconsumptionVo.getFueloilConvStandardcoal() +
                        productYearEnergyconsumptionVo.getWaterConvStandardcoal());

        // 算出单位产品能耗
        productYearEnergyconsumptionVo.setYearYield(
                productYearEnergyconsumptionVo.getEnTotal() / productYearEnergyconsumptionVo.getYearYield());

        // 算出能耗强度
        productYearEnergyconsumptionVo.setEnStrength(
                productYearEnergyconsumptionVo.getEnTotal() / productYearEnergyconsumptionVo.getYearSalesIncome());

        return productYearEnergyconsumptionVo;

    }
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ProductYearEnergyconsumptionVo queryById(Long id) {
        return this.productYearEnergyconsumptionDao.queryById(id);
    }

    /**
     * 通过ID和关联的企业用户id查询单条数据
     *
     * @param id 主键
     * @param userId 关联的企业用户id
     * @return 实例对象
     */
    @Override
    public ProductYearEnergyconsumptionVo queryByIdAndUserId(Long id, Integer userId) {
        return this.productYearEnergyconsumptionDao.queryByIdAndUserId(id, userId);
    }

    /**
     * 通过产品id查询单条数据
     *
     * @param productId 主键
     * @return 实例对象
     */
    @Override
    public ProductYearEnergyconsumption queryByProductId(Long productId) {
        return this.productYearEnergyconsumptionDao.queryByProductId(productId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ProductYearEnergyconsumptionVo> queryAllByLimit(int offset, int limit) {
        return this.productYearEnergyconsumptionDao.queryAllByLimit(offset, limit);
    }

    /**
     * 对产品年能耗list中的列单项进行统计存储对象
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return
     */
    @Override
    public ProductYearEnergyconsumptionVoResult queryAllByLimitSumResult(int offset, int limit){
        return this.productYearEnergyconsumptionDao.queryAllByLimitSumResult(offset, limit);
    }

    /**
     * 关联企业用户id查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @param userId 关联企业用户id
     * @return 对象列表
     */
    @Override
    public List<ProductYearEnergyconsumptionVo> queryAllByLimitAndUserId(int offset, int limit,Integer userId) {
        return this.productYearEnergyconsumptionDao.queryAllByLimitAndUserId(offset, limit,userId);
    }

    /**
     * 使用关联用户id 对产品年能耗list中的列单项进行统计存储对象
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @param userId 关联企业用户id
     * @return
     */
    @Override
    public ProductYearEnergyconsumptionVoResult queryAllByLimitAndUserIdSumResult(int offset, int limit, Integer userId){
        return this.productYearEnergyconsumptionDao.queryAllByLimitAndUserIdSumResult(offset, limit,userId);
    }

    /**
     * 新增数据
     *
     * @param productYearEnergyconsumption 实例对象
     * @return 实例对象
     */
    @Override
    public ProductYearEnergyconsumption insert(ProductYearEnergyconsumption productYearEnergyconsumption) {
        this.productYearEnergyconsumptionDao.insert(productYearEnergyconsumption);
        return productYearEnergyconsumption;
    }

    /**
     * 修改数据
     *
     * @param productYearEnergyconsumption 实例对象
     * @return 实例对象
     */
    @Override
    public ProductYearEnergyconsumption update(ProductYearEnergyconsumption productYearEnergyconsumption) {
        this.productYearEnergyconsumptionDao.update(productYearEnergyconsumption);
        return this.queryById(productYearEnergyconsumption.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.productYearEnergyconsumptionDao.deleteById(id) > 0;
    }

    /**
     * 关键字 用户id 查询产品年能耗列表
     * @param search
     * @param start
     * @param length
     * @param userid
     * @return
     */
    @Override
    public List<ProductYearEnergyconsumptionVo> queryAllByKeywordAndLimitAndUserId(String search, Integer start, Integer length, Integer userid){
        return this.productYearEnergyconsumptionDao.queryAllByKeywordAndLimitAndUserId(search, start, length, userid);
    }
    /**
     * 关键字查询查询产品年能耗列表分页
     * @param search
     * @param start
     * @param length
     * @return
     */
    @Override
    public List<ProductYearEnergyconsumptionVo> queryAllByLimitAndKeyword(String search, Integer start, Integer length){
        return this.productYearEnergyconsumptionDao.queryAllByLimitAndKeyword(search, start, length);
    }

    /**
     * 关键字和用户id查询产品年能耗列表分页
     * @param search
     * @param userId
     * @return
     */
    @Override
    public List<ProductYearEnergyconsumptionVo> queryAllByKeywordAndUserId(String search, Integer userId){
        return this.productYearEnergyconsumptionDao.queryAllByKeywordAndUserId(search, userId);
    }

    /**
     * 用户id查询产品年能耗列表
     * @param userId
     * @return
     */
    @Override
    public List<ProductYearEnergyconsumptionVo> queryAllByUserId(Integer userId){
        return this.productYearEnergyconsumptionDao.queryAllByUserId(userId);
    }

    /**
     * 关键字查询产品年能耗列表
     * @param search
     * @return
     */
    @Override
    public List<ProductYearEnergyconsumptionVo> queryAllByKeyword(String search){
        return this.productYearEnergyconsumptionDao.queryAllByKeyword(search);
    }

    /**
     * 查询所有产品年能耗列表
     * @return
     */
    @Override
    public List<ProductYearEnergyconsumptionVo> queryAll(){
        return this.productYearEnergyconsumptionDao.queryAll2();
    }

    /**
     * 产品id查询 产品年能耗list
     * @param productId
     * @return
     */
    @Override
    public List<ProductYearEnergyconsumptionVo> queryListByProductId(Long productId){
        return this.productYearEnergyconsumptionDao.queryListByProductId(productId);
    }
}
